package com.imooc.scala.log

import java.text.SimpleDateFormat
import java.util.{Date, Locale}

import org.apache.commons.lang3.time.FastDateFormat

//import org.apache.commons.lang3.time.FastDateFormat


object DateUtils {

  //19/Aug/2019:10:43:40 +0000
  //  val YYYYMMDDHHMM = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss Z", Locale.ENGLISH)
  val YYYYMMDDHHMM = FastDateFormat.getInstance("dd/MMM/yyyy:HH:mm:ss Z", Locale.ENGLISH)
  //  val TARGET_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
  val TARGET_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss")

  def parse(time: String) = {
    TARGET_FORMAT.format(new Date(getTime(time)))

  }

  def getTime(time: String) = {
    try {
      YYYYMMDDHHMM.parse(time.substring(time.indexOf("[") + 1, time.lastIndexOf("]"))).getTime()
    } catch {
      case e: Exception => {
        0l
      }
    }
  }

  def main(args: Array[String]): Unit = {
    println(parse("[19/Aug/2019:10:43:40 +0000]"))
  }
}
